//
//  MyImageView.m
//  LimitFree
//
//  Created by Xuan on 14-5-2.
//  Copyright (c) 2014年 Xuan. All rights reserved.
//

#import "MyImageView.h"
@interface MyImageView ()
{
//     UIButton * _button;
}
@property (nonatomic,assign) id target;
@property (nonatomic,assign) SEL action;

@end

@implementation MyImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //打开用户交互
        self.userInteractionEnabled = YES;
        _button = [UIButton buttonWithType:UIButtonTypeSystem];
        _button.frame = CGRectMake(0, 0, 35, 35);
        [_button setBackgroundImage:[UIImage imageNamed:@"delete1"] forState:UIControlStateNormal];
        //
        _button.hidden = YES;
        //
        [_button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_button];
    }
    return self;
}
//
-(void)buttonClick:(UIButton *)button{
    //删除按钮的点击事件，
    if([self.delegate respondsToSelector:@selector(myImageView:deleteButton:)]){
        [self.delegate myImageView:self deleteButton:_button];
    }else{
        //代理没有实现协议中方法
    }
}
//
-(void)setDeleteButtonHidden:(BOOL)hidden{
    _button.hidden = hidden;
}
//触摸屏幕离开的时候调用
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    if ([self.target respondsToSelector:self.action]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [self.target performSelector:self.action withObject:self];//此处是你调用函数的地方
#pragma clang diagnostic pop

    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

}
- (void)addTarget:(id)target action:(SEL)action{
    self.target = target;
    self.action = action;
}
//书名
- (void)setTitleWithStr:(NSString *)str{
    _nameStr=[str copy];
    if (!_titleLabel) {
        _titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(4, ([self getView_Height]-60)/2, [self getView_Width]-18, 60)];
        _titleLabel.font=FONT_12;
        _titleLabel.text=str;
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.numberOfLines=0;
        _titleLabel.textAlignment=NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
    }else{
        _titleLabel.text=str;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
